package blog.spring.db.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import blog.core.exception.FormParamErrorException;
import blog.core.exception.NotFoundException;
import blog.core.util.ExceptionUtils;
import blog.spring.db.dao.ITypeDAO;
import blog.spring.db.entity.Type;
import blog.spring.db.service.ITypeService;

@Service("typeService")
public class TypeService extends BaseService<Type> implements ITypeService {

	public enum DescribeType {// 类别的分类
		ARTICLE(1), MESSAGE(2);

		private int value;

		private DescribeType(int value) {
			this.value = value;
		}

		public static DescribeType valueOf(int value) { // 手写的从int到enum的转换函数
			switch (value) {
			case 1:
				return ARTICLE;
			case 2:
				return MESSAGE;
			default:
				return null;
			}
		}

		public int value() {
			return this.value;
		}
	}

	public static final String TYPE_NAME = "类别";
	private final ITypeDAO typeDao;

	@Autowired
	public TypeService(ITypeDAO typeDao) {
		super(typeDao, TYPE_NAME);
		this.typeDao = typeDao;
	}

	@Override
	public void save(Type entity) throws FormParamErrorException {
		// TODO Auto-generated method stub
		super.save(entity);
	}

	@Override
	public List<Type> getTypeListAll(DescribeType type) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("describeType", type.value());
		return searchList(conditions, null, null, null, null, false);
	}

	@Override
	public void remove(Type entity) {
		if (entity.getArticleInfos().size() > 0)
			ExceptionUtils.NewDepend(TYPE_NAME, ArticleService.TYPE_NAME);
		if (entity.getMessages().size() > 0)
			ExceptionUtils.NewDepend(TYPE_NAME, "消息");
		super.remove(entity);
	}

	@Override
	public void update(Type entity) throws FormParamErrorException,
			NotFoundException {
		Type ptType = findById(entity.getTypeId());
		ptType.setTitle(entity.getTitle());
		super.update(ptType);
	}
}